<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @package     Pages Module
 * @author      Matrix Infoline (infolinematrix@gmail.com
 * @copyright   (c) Matrix Infoline
 */

class Payment extends Frontend_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('Paypal');
    }

    function paypal($pID=0,$name='',$amt=0,$proID=0)

    {

    $this->paypal->initialize();

    //$this->paypal->add_field( 'return', site_url( 'member/payment/success/'.$pID.'/'.$proID ) );


     $this->paypal->add_field( 'return', site_url('member/payment/ipn/'.$pID.'/'.$proID));
     $this->paypal->add_field( 'cancel_return', site_url( 'member/payment/cancel' ) );


    $this->paypal->add_field( 'item_name', $name);
    $this->paypal->add_field( 'amount', $amt );
    $this->paypal->add_field( 'quantity', '1');

    $this->paypal->paypal_auto_form();

    //$rr = $this->paypal->validate_ipn();
    //print_r($rr);

    }

    public function ipn($pID=0,$proID=0) {


      //  die();

    if ( $this->paypal->validate_ipn() ) {





        $pdata = $this->paypal->ipn_data;
        if ($pdata['txn_type'] == "web_accept") {
            if($pdata['payment_status'] == "Completed"){

                $sql = "select * from paypal limit 0,1";
                $result=$this->db->query($sql)->row();

                if($pdata['business'] == $result->user_id) {
                    //handle payment...
                }
            }
            redirect('member/payment/success/'.$pID.'/'.$proID);
        }else{

            redirect('member/payment/cancel');

        }


    }

    }


    public function success($pID=0,$proID=0) {

    $sql = "update post set priority = 1,plan=$pID where id=$proID";
    $this->db->query($sql);

    // var_dump($sql);die();

        $data['title'] = "Payment Successfully";
        $data['mode'] = "PAYMENT";

        $this->template->set_layout('member_layout');
        $this->template->set_partial('header', 'header/headerTop');
        $this->member_model->check_logedin();
        $this->load->model('term_model');

         $this->template->set_breadcrumb('Home', '/')
        ->set_breadcrumb('Member','member/dashboard')
        ->set_breadcrumb('Payment Success');

         $this->template->build('business',$data);
    //redirect('member/profile/edit')

    }

    public function cancel() {
        $data['title'] = "Payment Canceled";
        $data['mode'] = "CANCEl";

        $this->template->set_layout('member_layout');
        $this->template->set_partial('header', 'header/headerTop');
        $this->member_model->check_logedin();
        $this->load->model('term_model');

        $this->template->set_breadcrumb('Home', '/')
        ->set_breadcrumb('Member','member/dashboard')
        ->set_breadcrumb('Payment Cancel');
        $this->template->build('business',$data);
    }

}